package com.liwang.lanouhn.nvshengo.fragments;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.liwang.lanouhn.nvshengo.Info.InfoEvent;
import com.liwang.lanouhn.nvshengo.Info.VideoInfo;
import com.liwang.lanouhn.nvshengo.R;
import com.liwang.lanouhn.nvshengo.adapter.SubscibeAdapter;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by lanouhn on 2016/10/24.
 */
public class SubscibeFragment extends Fragment {
    private RecyclerView rv_subscibe;
    private VideoInfo info;
    private List<VideoInfo> infos=new ArrayList<>();
    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_subscibe, null);
        initView(view);
        initEvent();
        return view;
    }
    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        EventBus.getDefault().register(this);
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(this);
    }
    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onInfoEvent(InfoEvent event){
        info=event.info;
        infos.add(info);
    }
//    @Subscribe(threadMode = ThreadMode.MAIN)
//    public void onReduceEvent(ReduceEvent event){
//        info=event.info;
//        infos.remove(info);
//    }


    private void initView(View view) {

        rv_subscibe = (RecyclerView) view.findViewById(R.id.rv_subscibe);
        GridLayoutManager gridLayoutManager =new GridLayoutManager(getActivity(),2);
        rv_subscibe.setLayoutManager(gridLayoutManager);
    }
    private void initEvent() {
        if (infos.size()>0){
            SubscibeAdapter adapter =new SubscibeAdapter(getActivity(),infos);
            rv_subscibe.setAdapter(adapter);
        }

    }

}
